/*
 * Copyright (c) 2007 T.Cogghe, D.Leroux, S.Lévignac, S.Rives
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#include "IHM/editAstres.hh"



void IHM::EditAstres::setImage(Image&i,Image::Seuil& k) {
	seuil=k;
	setImage(i);
}


void IHM::EditAstres::setImage(Image&i) {
	astab[0]=astab[1]=astab[2]=NULL;
	n_astres=0;

	if(backup) {
		delete backup;
	}
	backup=new Image(i.copiePixbuf());
	maj();
}



void IHM::EditAstres::ajouterAstre(int x,int y) {
	Astre*a;
	int doublon;
	if(n_astres==3) {
		return;
	}
	a=Astre::trouverAutourDe(*backup,x,y,seuil);
	if(a) {
		doublon=0;
		for(int i=0;i<n_astres;i++) {
			//if(astab[i]->x()==a->x()&&astab[i]->y()==a->y()) {
			if(astab[i]->id()==a->id()) {
				doublon=1;
			}
		}
		if(!doublon) {
			astab[n_astres]=a;
			n_astres++;
			maj();
		}
	}
	if(n_astres==3) {
		if(curseur) delete curseur;
		curseur=NULL;
		degriserValid();
	}
}


#define NO_DIST 1.e50

void IHM::EditAstres::enleverAstre(int x,int y) {
	double dist=NO_DIST,dx,dy,tmp;
	int imin=-1;

	for(int i=0;i<n_astres;i++) {
		dx=astab[i]->x()-x;
		dy=astab[i]->y()-y;
		tmp=sqrt(dx*dx+dy*dy);
		if(tmp<dist) {
			imin=i;
			dist=tmp;
		}
	}
	if(imin==0) {
		delete astab[0];
		astab[0]=astab[1];
		astab[1]=astab[2];
		astab[2]=NULL;
		n_astres--;
		maj();
	} else if(imin==1) {
//		} else if(n_astres>1&&astab[1]->x()==a->x()&&astab[1]->y()==a->y()) {
		delete astab[1];
		astab[1]=astab[2];
		astab[2]=NULL;
		n_astres--;
		maj();
	} else if(imin==2) {
//		} else if(n_astres>2&&astab[2]->x()==a->x()&&astab[2]->y()==a->y()) {
		delete astab[2];
		astab[2]=NULL;
		n_astres--;
		maj();
	}
	if(n_astres==2) {
		/* parce qu'on était à trois astres avant, et que valid était dégrisé */
		griserValid();
	}
}



void IHM::EditAstres::maj() {
	Glib::RefPtr<Gdk::Pixbuf> gpb=backup->copiePixbuf();
	Image pic(gpb);

	dest->set(pic);
	for(int i=0;i<n_astres;i++) {
		if(astab[i]) {
			astab[i]->tracerContour(pic,2);
		} else {
			cerr<<"hey ! astab["<<i<<"] est NULL"<<endl;
		}
	}
	dest->queue_draw();
}



